<style>
.layui-input-block{
margin-left:120px;
}
</style>
<form class="layui-form" action="" id="orderEditForm">

  <div class="layui-form-item" style="padding-top:20px">
    <label class="layui-form-label col-sm-1" style="width:100px;"><font color='red' size="4">*</font>物料编码</label>
    <div class="layui-input-block col-sm-2" style="width:250px;padding-left:0px;margin-left:0px;">
    	<input type="text" class="layui-input" required name="materialCoding" lay-verify="required"/>
    </div>
    <label class="layui-form-label col-sm-1" style="width:100px;"><font color='red' size="4">*</font>物料名称</label>
    <div class="layui-input-block col-sm-2" style="width:250px;padding-left:0px;margin-left:0px;">
	    <input type="text" class="layui-input" required name="materialName" lay-verify="required"/>
    </div>
    <label class="layui-form-label col-sm-1" style="width:100px;">物料规格</label>
    <div class="layui-input-block col-sm-2" style="width:250px;padding-left:0px;margin-left:0px;">
    <input type="text" class="layui-input" required name="materialSpec" lay-verify="required"/>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label col-sm-1" style="width:100px;"><font color='red' size="4">*</font>物料类型</label>
    <div class="layui-input-block col-sm-2" style="width:250px;padding-left:0px;margin-left:0px;">
    	<select name="materialTypeId" lay-verify="required" lay-filter="materialTypeId" required >
	       <option th:each="s:${materialTypeList}" th:attr="value=${s.id}" th:text="${s. value}"></option>
	    </select>
    </div>
  	<label class="layui-form-label col-sm-1" style="width:100px;">品牌</label>
    <div class="layui-input-block col-sm-2" style="width:250px;padding-left:0px;margin-left:0px;">
    	<select name="brandId" lay-verify="required" lay-filter="brandId" required >
	       <option th:each="s:${materialBrandList}" th:attr="value=${s.id}" th:text="${s. value}"></option>
	    </select>
    </div>
  	<label class="layui-form-label col-sm-1" style="width:100px;"><font color='red' size="4">*</font>计价方式</label>
    <div class="layui-input-block col-sm-2" style="width:250px;padding-left:0px;margin-left:0px;">
    	<select name="materialPriceTypeId" lay-verify="required" lay-filter="materialPriceTypeId" required >
	       <option th:each="s:${priceTypeList}" th:attr="value=${s.id}" th:text="${s. value}"></option>
	    </select>
    </div>
  </div>
  <div class="layui-form-item">
  	<label class="layui-form-label col-sm-1" style="width:100px;"><font color='red' size="4">*</font>计量单位</label>
    <div class="layui-input-block col-sm-2" style="width:250px;padding-left:0px;margin-left:0px;">
    	<select name="measurementUId" lay-verify="required" lay-filter="measurementUId" required >
	       <option th:each="s:${unitList}" th:attr="value=${s.id}" th:text="${s.name}"></option>
	    </select>
    </div>
    <label class="layui-form-label col-sm-1" style="width:100px;">最小包装量</label>
    <div class="layui-input-block col-sm-2" style="width:250px;padding-left:0px;margin-left:0px;">
    	 <input type="text" class="layui-input" required name="minimumPackingQuantity" lay-verify="required" />
    </div>
  	<label class="layui-form-label col-sm-1" style="width:100px;"><font color='red' size="4">*</font>是否停用</label>
    <div class="layui-input-block col-sm-2" style="width:250px;padding-left:0px;margin-left:0px;">
      <input type="radio" name="isNoUsed" value="true" title="是">
      <input type="radio" name="isNoUsed" value="false" title="否" checked>
    <!-- <input type="checkbox" name="isNoUsed"  lay-skin="switch" lay-text="是|否"> -->
    	<!-- <select class="form-control input-sm formSubmit" name="isNoUsed" skey="isNoUsed">
            <option value="0">否</option>
            <option value="1">是</option>
        </select> -->
    </div>
  </div>
  
  <div class="layui-form-item">
  <label class="layui-form-label col-sm-1" style="width:100px;">是否委外</label>
    <div class="layui-input-block col-sm-2" style="width:250px;padding-left:0px;margin-left:0px;">
    	<input type="radio" name="isOutsource" value="true" title="是">
        <input type="radio" name="isOutsource" value="false" title="否" checked>
    	<!-- <select class="form-control input-sm formSubmit" name="isOutsource" skey="isOutsource">
            <option value="0">否</option>
            <option value="1">是</option>
        </select> -->
    </div>
  	<label class="layui-form-label col-sm-1" style="width:100px;">是否可销售</label>
    <div class="layui-input-block col-sm-2" style="width:250px;padding-left:0px;margin-left:0px;">
    	<input type="radio" name="isSale" value="true" title="是">
        <input type="radio" name="isSale" value="false" title="否" checked>
    	<!-- <select class="form-control input-sm formSubmit" name="isSale" skey="isSale">
            <option value="0">否</option>
            <option value="1">是</option>
        </select> -->
    </div>
  	<label class="layui-form-label col-sm-1" style="width:100px;">是否为外购</label>
    <div class="layui-input-block col-sm-2" style="width:250px;padding-left:0px;margin-left:0px;">
    	<input type="radio" name="isBuy" value="true" title="是">
        <input type="radio" name="isBuy" value="false" title="否" checked>
    	<!-- <select class="form-control input-sm formSubmit" name="isBuy" skey="isBuy">
            <option value="0">否</option>
            <option value="1">是</option>
        </select> -->
    </div>
  </div>
  <div class="layui-form-item">
  	<label class="layui-form-label col-sm-1" style="width:100px;">是否生产用</label>
    <div class="layui-input-block col-sm-2" style="width:250px;padding-left:0px;margin-left:0px;">
    	<input type="radio" name="isProdConsumption" value="true" title="是">
        <input type="radio" name="isProdConsumption" value="false" title="否" checked>
    	<!-- <select class="form-control input-sm formSubmit" name="isProdConsumption" skey="isProdConsumption">
            <option value="0">否</option>
            <option value="1">是</option>
        </select> -->
    </div>
  	<label class="layui-form-label col-sm-1" style="width:100px;">是否虚拟件</label>
    <div class="layui-input-block col-sm-2" style="width:250px;padding-left:0px;margin-left:0px;">
    	<input type="radio" name="isVirtual" value="true" title="是">
        <input type="radio" name="isVirtual" value="false" title="否" checked>
    	<!-- <select class="form-control input-sm formSubmit" name="isVirtual" skey="isVirtual">
            <option value="0">否</option>
            <option value="1">是</option>
        </select> -->
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label col-sm-1 " style="width:100px;" >备注</label>
    <div class="layui-input-block col-sm-2" style="width:90%;padding-left:0px;margin-left:0px;">
      <input type="text" class="layui-input" required name="remark" lay-verify="required"/>
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
          <button class="layui-btn" lay-submit="" lay-filter="formSubmit" authcode='900229'>提交</button>
      	  <button type="button" class="layui-btn layui-btn-primary" id="closeBtn">关闭</button>
    </div>
  </div>

</form>
<script>
layui.use('form', function(){
	 $("[name='supplyId']").select2({
		  placeholder:"请选择供应商",//placeholder
		  language: 'zh-CN',//中文语言
		  //allowClear:true,//出现删除按钮图片，可以清除下拉选中内容
		  ajax: {
			    url: "/com/suppliercustomer/supplierList",
			    dataType: 'json',
			    delay: 350,
			    data: function (params) {
			      console.log(params.page)
			      return {
			        name: params.term, // search term
			        companyid:1,
			        page: params.page,
			        limit:30
			      };
			    },
			    processResults: function (data, params) {
			      params.page = params.page || 1;
			      //console.log(JSON.stringify(data))
			      return {
			        results: data.data,
			        pagination: {
			          more: (params.page * 30) < data.count
			        }
			      };
			    },
			    cache: true
			  },
			  escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
			  minimumInputLength: 0,//最小输入字符，输入最小字符后开始后台请求
			  templateResult: formatRepo, // 处理远程返回数据
			  templateSelection: formatRepoSelection // 设置下拉选中后显示内容
			});
	  
	  	function formatRepo (repo) {
	  		//下拉展示内容可以修改自定义样式
	      if (repo.loading) return repo.text;
	      var markup = "<div class=\"layui-row\" style='width:250px;'>" +
			      "<div class=\"layui-col-xs4 layui-col-sm4 layui-col-md3\">" +
			      	"<div class=\"grid-demo grid-demo-bg1\">"+repo.no+"</div>" +
			     "</div>"+
			     "<div class=\"layui-col-xs8 layui-col-sm8 layui-col-md9\">" +
			      	"<div class=\"grid-demo grid-demo-bg1\">"+repo.name+"</div>" +
			     "</div>";

	      return markup;
	    }

	    function formatRepoSelection (repo) {
	      return repo.name;//选中返回select中的值
	    }
	  var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
	  form.render();
	  //select2选中事件
	  $("[name='supplyId']").on("select2:select",function(e){
		  var id = e.params.data.id;
		  $.getJSON("/com/suppliercustomer/contacts?supplierCustomerId="+id,function(data){
			  var html = [];
			  html.push("<option value=''>请选择联系人</option>")
			  $.each(data,function(i,item){
				  html.push("<option value='"+item.id+"' data-phone='"+item.phone+"'>"+item.name+"</option>")
			  })
			  $("select[name='supplyContactId']").html(html.join(""));
             form.render('select'); //这个很重要
		  });
	  })
	
	  form.on('select(supplyContactId)', function(data){
		  var phone = $(data.elem).find("option[value='"+data.value+"']").attr("data-phone");
		  $("input[name='supplyContactPhone']").val(phone);
		});      
	  //监听提交
	  form.on('submit(formSubmit)', function(data){
	    $.ajax({
            type: "GET",
            url: "/eng/material/info/addMaterial",
            data: $("#orderEditForm").serializeJson(),
            dataType: "json",
            success: function(data){
            	if(data.code==200){
            	toastr.success(data.msg);
            	ins.reload({url:'/eng/material/info/page'})
            	//关闭弹出层
           	    layer.closeAll(editdialogindex);
           	    //更新表格数据
            	}else{
            		layer.msg(data.message);
            		//关闭
            	    layer.closeAll();
            	}
            }
           });
	  });
	  //关闭按钮
	  $("#closeBtn").click(function(){
		  layer.closeAll();
	  })
	});      
</script>
